home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / SCFDGEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  7.6 KB  |  202 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* scfdgen.c */
  21. /* Generate the CCITTFaxDecode tables */
  22. #include <stdio.h>
  23. #include "std.h"
  24. #include "scf.h"
  25. #include "malloc_.h"
  26. #include "memory_.h"
  27.  
  28. typedef void (*cfd_node_proc)(P6(cfd_node *, cfd_node *,
  29.                  uint, int, int, int));
  30. typedef void (*cfd_enum_proc)(P4(cfd_node_proc,
  31.                  cfd_node *, cfd_node *, int));
  32. private void cfd_build_tree(P4(cfd_node *, cfd_enum_proc, int, FILE *));
  33. private void cfd_enumerate_white(P4(cfd_node_proc,
  34.                     cfd_node *, cfd_node *, int));
  35. private void cfd_enumerate_black(P4(cfd_node_proc,
  36.                     cfd_node *, cfd_node *, int));
  37. private void cfd_enumerate_2d(P4(cfd_node_proc,
  38.                  cfd_node *, cfd_node *, int));
  39. private void cfd_enumerate_uncompressed(P4(cfd_node_proc,
  40.                        cfd_node *, cfd_node *, int));
  41.  
  42. main()
  43. {    FILE *out = fopen("scfdtab.c", "w");
  44.     cfd_node area[1 << max(cfd_white_initial_bits, cfd_black_initial_bits)];
  45.     fputs("/* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved. */\n\n", out);
  46.     fputs("/* scfdtab.c */\n", out);
  47.     fputs("/* Tables for CCITTFaxDecode filter. */\n\n", out);
  48.     fputs("#include \"std.h\"\n", out);
  49.     fputs("#include \"scf.h\"\n\n", out);
  50.     fputs("/* White decoding table. */\n", out);
  51.     fputs("const cfd_node cf_white_decode[] = {\n", out);
  52.     cfd_build_tree(area, cfd_enumerate_white, cfd_white_initial_bits, out);
  53.     fputs("\n};\n\n", out);
  54.     fputs("/* Black decoding table. */\n", out);
  55.     fputs("const cfd_node cf_black_decode[] = {\n", out);
  56.     cfd_build_tree(area, cfd_enumerate_black, cfd_black_initial_bits, out);
  57.     fputs("\n};\n\n", out);
  58.     fputs("/* 2-D decoding table. */\n", out);
  59.     fputs("const cfd_node cf_2d_decode[] = {\n", out);
  60.     cfd_build_tree(area, cfd_enumerate_2d, cfd_2d_initial_bits, out);
  61.     fputs("\n};\n\n", out);
  62.     fputs("/* Uncompresssed decoding table. */\n", out);
  63.     fputs("const cfd_node cf_uncompressed_decode[] = {\n", out);
  64.     cfd_build_tree(area, cfd_enumerate_uncompressed, cfd_uncompressed_initial_bits, out);
  65.     fputs("\n};\n\n", out);
  66.     fputs("/* Dummy executable code to pacify compilers. */\n", out);
  67.     fputs("void\ncfd_dummy()\n{\n}\n", out);
  68.     fclose(out);
  69.     return 0;
  70. }
  71.  
  72. /* Initialize first-level leaves, count second-level nodes. */
  73. private void
  74. cfd_count_nodes(cfd_node *tree, cfd_node *ignore_extn,
  75.   uint code, int code_length, int run_length, int initial_bits)
  76. {    if ( code_length <= initial_bits )
  77.     {    /* Initialize one or more first-level leaves. */
  78.         int sh = initial_bits - code_length;
  79.         cfd_node *np = &tree[code << sh];
  80.         int i;
  81.         for ( i = 1 << sh; i > 0; i--, np++ )
  82.             np->run_length = run_length,
  83.             np->code_length = code_length;
  84.     }
  85.     else
  86.     {    /* Note the need for a second level. */
  87.         cfd_node *np = &tree[code >> (code_length - initial_bits)];
  88.         np->code_length = max(np->code_length, code_length);
  89.     }
  90. }
  91.  
  92. /* Initialize second-level nodes. */
  93. private void
  94. cfd_init2_nodes(cfd_node *tree, cfd_node *extn,
  95.   uint code, int code_length, int run_length, int initial_bits)
  96. {    int xbits = code_length - initial_bits;
  97.     int xrep;
  98.     cfd_node *np1, *np2;
  99.     int i;
  100.     if ( xbits <= 0 ) return;
  101.     np1 = &tree[code >> xbits];
  102.     np2 = &extn[np1->run_length - (1 << initial_bits)];
  103.     xrep = np1->code_length - code_length;
  104.     i = 1 << xrep;
  105.     np2 += (code & ((1 << xbits) - 1)) * i;
  106.     for ( ; i > 0; i--, np2++ )
  107.         np2->run_length = run_length,
  108.         np2->code_length = xbits;
  109. }
  110.  
  111. /* Enumerate all the relevant white or black codes. */
  112. private void
  113. cfd_enumerate_codes(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  114.   int initial_bits, const cfe_run *tt, const cfe_run *mut)
  115. {    int i;
  116.     const cfe_run *ep;
  117.     for ( i = 0, ep = tt; i < 64; i++, ep++ )
  118.       (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  119.     for ( i = 1, ep = mut + 1; i < 41; i++, ep++ )
  120.       (*proc)(tree, extn, ep->code, ep->code_length, i << 6, initial_bits);
  121.     (*proc)(tree, extn, cf1_run_uncompressed.code, cf1_run_uncompressed.code_length, run_uncompressed, initial_bits);
  122.     (*proc)(tree, extn, 0, run_eol_code_length - 1, run_zeros, initial_bits);
  123. }
  124. private void
  125. cfd_enumerate_white(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  126.   int initial_bits)
  127. {    cfd_enumerate_codes(proc, tree, extn, initial_bits,
  128.                 cf_white_termination, cf_white_make_up);
  129. }
  130. private void
  131. cfd_enumerate_black(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  132.   int initial_bits)
  133. {    cfd_enumerate_codes(proc, tree, extn, initial_bits,
  134.                 cf_black_termination, cf_black_make_up);
  135. }
  136.  
  137. /* Enumerate the 2-D codes. */
  138. private void
  139. cfd_enumerate_2d(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  140.   int initial_bits)
  141. {    int i;
  142.     const cfe_run *ep;
  143.     (*proc)(tree, extn, cf2_run_pass.code, cf2_run_pass.code_length,
  144.         run2_pass, initial_bits);
  145.     (*proc)(tree, extn, cf2_run_horizontal.code, cf2_run_horizontal.code_length,
  146.         run2_horizontal, initial_bits);
  147.     for ( i = 0; i < countof(cf2_run_vertical); i++ )
  148.     {    ep = &cf2_run_vertical[i];
  149.         (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  150.     }
  151.     (*proc)(tree, extn, cf2_run_uncompressed.code, cf2_run_uncompressed.code_length,
  152.         run_uncompressed, initial_bits);
  153.     (*proc)(tree, extn, 0, run_eol_code_length - 1, run_zeros, initial_bits);
  154. }
  155.  
  156. /* Enumerate the uncompressed codes. */
  157. private void
  158. cfd_enumerate_uncompressed(cfd_node_proc proc, cfd_node *tree, cfd_node *extn,
  159.   int initial_bits)
  160. {    int i;
  161.     const cfe_run *ep;
  162.     for ( i = 0; i < countof(cf_uncompressed); i++ )
  163.     {    ep = &cf_uncompressed[i];
  164.         (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  165.     }
  166.     for ( i = 0; i < countof(cf_uncompressed_exit); i++ )
  167.     {    ep = &cf_uncompressed_exit[i];
  168.         (*proc)(tree, extn, ep->code, ep->code_length, i, initial_bits);
  169.     }
  170. }
  171.  
  172. /* Build and write out the table. */
  173. private void
  174. cfd_build_tree(cfd_node *tree, cfd_enum_proc enum_proc, int initial_bits,
  175.   FILE *f)
  176. {    cfd_node *np;
  177.     const char *prev = "";
  178.     int i, next;
  179.     cfd_node *extn;
  180.     memset(tree, 0, sizeof(cfd_node) << initial_bits);
  181.     /* Construct and write the first level of the tree. */
  182.     (*enum_proc)(cfd_count_nodes, tree, (cfd_node *)0, initial_bits);
  183.     next = 0;
  184.     for ( i = 0, np = tree; i < 1 << initial_bits; i++, np++ )
  185.     { if ( np->code_length > initial_bits )        /* second level needed */
  186.       { np->run_length = next + (1 << initial_bits);
  187.         next += 1 << (np->code_length - initial_bits);
  188.       }
  189.       fprintf(f, "%s\t{ %d, %d }", prev, np->run_length, np->code_length);
  190.       prev = ",\n";
  191.     }
  192.     /* Construct and write the second level. */
  193.     extn = (cfd_node *)malloc(sizeof(cfd_node) * next);
  194.     for ( i = 0, np = extn; i < next; i++, np++ )
  195.         np->run_length = run_error,
  196.         np->code_length = 0;
  197.     (*enum_proc)(cfd_init2_nodes, tree, extn, initial_bits);
  198.     for ( i = 0, np = extn; i < next; i++, np++ )
  199.       fprintf(f, ",\n\t{ %d, %d }", np->run_length, np->code_length);
  200.     free((char *)extn);
  201. }
  202.